package com.atguigu.spzx.order.service.impl;

import com.atguigu.spzx.common.exception.SPZXException;
import com.atguigu.spzx.common.util.AuthContextUtil;
import com.atguigu.spzx.feign.cart.CartFeignClient;
import com.atguigu.spzx.feign.product.ProductFeignClient;
import com.atguigu.spzx.feign.user.UserFeignClient;
import com.atguigu.spzx.model.dto.h5.OrderInfoDto;
import com.atguigu.spzx.model.entity.h5.CartInfo;
import com.atguigu.spzx.model.entity.order.OrderInfo;
import com.atguigu.spzx.model.entity.order.OrderItem;
import com.atguigu.spzx.model.entity.order.OrderLog;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.entity.user.UserAddress;
import com.atguigu.spzx.model.entity.user.UserInfo;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.h5.TradeVo;
import com.atguigu.spzx.order.mapper.OrderInfoMapper;
import com.atguigu.spzx.order.mapper.OrderItemMapper;
import com.atguigu.spzx.order.mapper.OrderLogMapper;
import com.atguigu.spzx.order.service.OrderInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderInfoServiceImp implements OrderInfoService {

    @Autowired
    private CartFeignClient cartFeignClient ;

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private OrderLogMapper orderLogMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Override
    public TradeVo getTrade() {
        List<CartInfo> cartInfoList = cartFeignClient.getAllCkecked();
        List<OrderItem> orderItemList = new ArrayList<>();
        for (CartInfo cartInfo : cartInfoList) {        // 将购物项数据转换成功订单明细数据
            OrderItem orderItem = new OrderItem();
            orderItem.setSkuId(cartInfo.getSkuId());
            orderItem.setSkuName(cartInfo.getSkuName());
            orderItem.setSkuNum(cartInfo.getSkuNum());
            orderItem.setSkuPrice(cartInfo.getCartPrice());
            orderItem.setThumbImg(cartInfo.getImgUrl());
            orderItemList.add(orderItem);
        }
        // 计算总金额
        BigDecimal totalAmount = new BigDecimal(0);
        for(OrderItem orderItem : orderItemList) {
            totalAmount = totalAmount.add(orderItem.getSkuPrice().multiply(new BigDecimal(orderItem.getSkuNum())));
        }
        TradeVo tradeVo = new TradeVo();
        tradeVo.setTotalAmount(totalAmount);
        tradeVo.setOrderItemList(orderItemList);
        return tradeVo;
    }

    @Override
    public Long submitOrder(OrderInfoDto orderInfoDto) {
        // 数据校验
        List<OrderItem> orderItemList = orderInfoDto.getOrderItemList();
        if(CollectionUtils.isEmpty(orderItemList)){
            throw new SPZXException(ResultCodeEnum.DATA_ERROR);
        }
        for(OrderItem orderItem : orderItemList) {
            ProductSku productSku = productFeignClient.getBySkuId(orderItem.getSkuId());
            if (null == productSku) {
                throw new SPZXException(ResultCodeEnum.DATA_ERROR);
            }
            //校验库存
            if (orderItem.getSkuNum().intValue() > productSku.getStockNum().intValue()) {
                throw new SPZXException(ResultCodeEnum.STOCK_LESS);
            }
        }
            // 构建订单数据，保存订单
            UserInfo userInfo = (UserInfo)AuthContextUtil.get();
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setOrderNo(String.valueOf(System.currentTimeMillis()));
            //用户id
            orderInfo.setUserId(userInfo.getId());
            //用户昵称
            orderInfo.setNickName(userInfo.getNickName());
            //用户收货地址信息
            UserAddress userAddress = userFeignClient.getUserAddress(orderInfoDto.getUserAddressId());
            orderInfo.setReceiverAddress(userAddress.getAddress());
            orderInfo.setReceiverPhone(userAddress.getPhone());
            orderInfo.setReceiverTagName(userAddress.getTagName());
            orderInfo.setReceiverProvince(userAddress.getProvinceCode());
            orderInfo.setReceiverCity(userAddress.getCityCode());
            orderInfo.setReceiverDistrict(userAddress.getDistrictCode());
            orderInfo.setReceiverAddress(userAddress.getFullAddress());
        //订单金额
        BigDecimal totalAmount = new BigDecimal(0);
        List<OrderItem> orderItemList1 = orderInfoDto.getOrderItemList();
        for(OrderItem orderItem : orderItemList1){
            totalAmount = totalAmount.add(orderItem.getSkuPrice().multiply( new BigDecimal(orderItem.getSkuNum())));
        }

        orderInfo.setTotalAmount(totalAmount);
        orderInfo.setCouponAmount(new BigDecimal(0));
        orderInfo.setOriginalTotalAmount(totalAmount);//原始总金额
        orderInfo.setFeightFee(orderInfoDto.getFeightFee());//运费
        orderInfo.setPayType(2);//支付方式【1->微信 2->支付宝】
        orderInfo.setOrderStatus(0);//订单状态【0->待付款；1->待发货；2->已发货；3->待用户收货，已完成；-1->已取消】

        orderInfo.setRemark(orderInfoDto.getRemark());
        orderInfoMapper.save(orderInfo);//useGeneratedKeys="true" keyProperty="id"主键回填

            for(OrderItem orderItem : orderItemList){
                orderItem.setOrderId(orderInfo.getId());
                orderItemMapper.save(orderItem);
        }
            //记录日志
        OrderLog orderLog = new OrderLog();
        orderLog.setOrderId(orderInfo.getId());
        orderLog.setProcessStatus(0);
        orderLog.setNote("提交订单");
        orderLogMapper.save(orderLog);
        // 5、清空购物车数据
        cartFeignClient.deleteChecked() ;
        return orderInfo.getId();
    }

    @Override
    public OrderInfo getOrderInfo(Long orderId) {
        return orderInfoMapper.getById(orderId);
    }

    @Override
    public TradeVo buy(Long skuId) {
        // 查询商品
        ProductSku productSku = productFeignClient.getBySkuId(skuId);
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        orderItem.setSkuId(skuId);
        orderItem.setSkuName(productSku.getSkuName());
        orderItem.setSkuNum(1);
        orderItem.setSkuPrice(productSku.getSalePrice());
        orderItem.setThumbImg(productSku.getThumbImg());
        orderItemList.add(orderItem);

        // 计算总金额
        BigDecimal totalAmount = productSku.getSalePrice();
        TradeVo tradeVo = new TradeVo();
        tradeVo.setTotalAmount(totalAmount);
        tradeVo.setOrderItemList(orderItemList);

        // 返回
        return tradeVo;
    }

    @Override
    public PageInfo<OrderInfo> findUserPage(Integer page, Integer limit, Integer orderStatus) {
        PageHelper.startPage(page,limit);
        UserInfo userInfo = (UserInfo)AuthContextUtil.get();
        Long userId = userInfo.getId();
        List<OrderInfo> orderInfoList = orderInfoMapper.findUserPage(userId,orderStatus);
          orderInfoList = orderInfoList.stream().map(orderInfo -> {
               orderInfo.setOrderItemList(orderItemMapper.findByOrderId(orderInfo.getId()));
                      return orderInfo;
           }
          ).collect(Collectors.toList());
        return new PageInfo<>(orderInfoList);
    }

    @Override
    public OrderInfo getByOrderNo(String orderNo) {
      OrderInfo orderInfo = orderInfoMapper.getByOrderNo(orderNo);
      List<OrderItem> orderItemList = orderItemMapper.findByOrderId(orderInfo.getId());
      orderInfo.setOrderItemList(orderItemList);
      return orderInfo;
    }

    @Transactional
    @Override
    public void updateOrderStatus(String orderNo, Integer payType) {
        // 更新订单状态
        OrderInfo orderInfo = orderInfoMapper.getByOrderNo(orderNo);
        orderInfo.setOrderStatus(1);
        orderInfo.setPayType(payType);
        orderInfo.setPaymentTime(new Date());
        orderInfoMapper.updateById(orderInfo);

        // 记录日志
        OrderLog orderLog = new OrderLog();
        orderLog.setOrderId(orderInfo.getId());
        orderLog.setProcessStatus(1);
        orderLog.setNote("支付宝支付成功");
        orderLogMapper.save(orderLog);
    }
}
